from django.contrib.auth.models import AbstractUser
from django.db import models

from dvadmin.utils.models import CoreModel
from ops.utils.models import table_prefix
from django.db.models import CharField, ForeignKey, CASCADE, SET_NULL, PROTECT

# 上线单
class Ticket(CoreModel):

    # 状态：0新建提交，1审核通过，2审核拒绝，3上线中，4上线完成，5上线失败
    status_choice = (
        ('0', '新建提交'),
        ('1', '审核通过'),
        ('2', '审核拒绝'),
        ('3', '上线中'),
        ('4', '上线完成'),
        ('5', '上线失败'),
    )

    # 类型
    type_choice = (
        ('0', '服务上线'),
        ('1', 'sql执行'),
    )

    name = models.CharField(max_length=64, verbose_name='任务名称')
    status = models.CharField(max_length=4, choices=status_choice, verbose_name='任务状态', help_text='任务状态', default=0, null=False, blank=True)
    approver = models.ForeignKey(to='system.Users', on_delete=CASCADE, verbose_name="审批人", related_name='task_approver',
                                db_constraint=False, null=True, blank=True)
    executor = models.ForeignKey(to='system.Users', on_delete=CASCADE, verbose_name="工单处理人", related_name='task_executor',
                                db_constraint=False, null=True, blank=True)
    type = models.CharField(max_length=4, choices=type_choice, verbose_name='任务类型', help_text='任务类型', default=0, null=False, blank=True)
    service = models.ForeignKey(to='config.Service', on_delete=CASCADE, verbose_name="关联服务", related_name='app_service',
                                db_constraint=False, null=True, blank=True)

    class Meta:
        # 自定义表名
        db_table = table_prefix + "deploy_ticket"
        verbose_name = '任务'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return f"{self.name}"